package com.e3.basdoc.goods.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.e3.basdoc.cust.client.CustRefDialog;
import com.e3.basdoc.employee.client.EmployeeRefDialog;
import com.e3.basdoc.goods.model.GoodsModel;
import com.e3.basdoc.goodscls.client.GoodsClsRefDialog;
import com.e3.basdoc.measure.client.MeasureUnitRefDialog;
import com.e3.basdoc.tax.client.TaxRefDialog;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class GoodsHelper extends GFHelper<GoodsModel>{
	
	public static GoodsHelper INSTANCE = new GoodsHelper();
	
	private String[] notNullFields = {GoodsModel.CODE,GoodsModel.NAME
			,GoodsModel.SHORT_NAME,GoodsModel.SPECIFICATION
			,GoodsModel.TYPE};
	private Map<String,Integer> lengthMap = new HashMap<String, Integer>();
	
	private GoodsHelper(){
		lengthMap.put(GoodsModel.ID, 19);
		lengthMap.put(GoodsModel.CODE, 20);
		lengthMap.put(GoodsModel.NAME, 50);
		lengthMap.put(GoodsModel.SHORT_NAME, 20);
		lengthMap.put(GoodsModel.FOREIGN_LANG_NAME, 50);
		lengthMap.put(GoodsModel.SPECIFICATION, 50);
		lengthMap.put(GoodsModel.TYPE, 50);
		lengthMap.put(GoodsModel.ORIGIN, 50);
		lengthMap.put(GoodsModel.BRAND, 50);
		lengthMap.put(GoodsModel.BARCODE, 50);
		lengthMap.put(GoodsModel.BULK, 19);
		lengthMap.put(GoodsModel.WEIGHT, 19);
		lengthMap.put(GoodsModel.REF_COST, 19);
		lengthMap.put(GoodsModel.REF_PRICE, 19);
		lengthMap.put(GoodsModel.FLOOR_PRICE, 19);
		lengthMap.put(GoodsModel.CEILING_PRICE, 19);
		lengthMap.put(GoodsModel.PURCHASE_LOSS_RATE, 4);
		lengthMap.put(GoodsModel.RESERVE_LOSS_RATE, 4);
		lengthMap.put(GoodsModel.EXPORT_REBATES_RATE, 4);
		
	}

	public List<Field<?>> getFormFields(String[] fNames){
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		
		if(name.equals(GoodsModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}	
		else if(name.equals(GoodsModel.CODE)||name.equals(GoodsModel.NAME)
			||name.equals(GoodsModel.SHORT_NAME)||name.equals(GoodsModel.FOREIGN_LANG_NAME)
			||name.equals(GoodsModel.SPECIFICATION)||name.equals(GoodsModel.TYPE)
			||name.equals(GoodsModel.ORIGIN)||name.equals(GoodsModel.BRAND)
			||name.equals(GoodsModel.BARCODE)){
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createStringField(name,dispName,length,isNotNull);
		}
		else if(name.equals(GoodsModel.CLASSES)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new GoodsClsRefDialog(),isNotNull);
		}
		else if(name.equals(GoodsModel.MEASURE_UNIT)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new MeasureUnitRefDialog(),isNotNull);
		}
		else if(name.equals(GoodsModel.TAX)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new TaxRefDialog(),isNotNull);
		}
		else if(name.equals(GoodsModel.MAIN_SUPPLIER)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new CustRefDialog(),isNotNull);
		}
		else if(name.equals(GoodsModel.SEAL_PERSON)){
			formField = MyFormFieldFactory.createRefField(name,dispName,new EmployeeRefDialog(),isNotNull);
		}
		else if(name.equals(GoodsModel.BULK)||name.equals(GoodsModel.WEIGHT)
				||name.equals(GoodsModel.REF_COST)||name.equals(GoodsModel.REF_PRICE)
				||name.equals(GoodsModel.FLOOR_PRICE)||name.equals(GoodsModel.CEILING_PRICE)
				||name.equals(GoodsModel.PURCHASE_LOSS_RATE)||name.equals(GoodsModel.RESERVE_LOSS_RATE)
				||name.equals(GoodsModel.EXPORT_REBATES_RATE)){
			int length = lengthMap.get(name);
			formField = MyFormFieldFactory.createDoubleField(name, dispName, 2,length,isNotNull);
		}
		else if(name.equals(GoodsModel.SEAL_TIME)){
			formField = MyFormFieldFactory.createDateField(name, dispName,isNotNull);
		}

		else if(name.equals(GoodsModel.ALLOW_NEGATIVE)){
			formField = MyFormFieldFactory.createYesNoRadioGroupField(name, dispName, "no");
		}
		else if(name.equals(GoodsModel.ALLOW_RETURN)){
			formField = MyFormFieldFactory.createYesNoRadioGroupField(name, dispName, "no");
		}
		else if(name.equals(GoodsModel.HAS_INVOICE)){
			formField = MyFormFieldFactory.createYesNoRadioGroupField(name, dispName, "no");
		}
		else if(name.equals(GoodsModel.SEAL_FLAG)){
			formField = MyFormFieldFactory.createYesNoRadioGroupField(name, dispName, "no");
		}
		return formField;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = GoodsModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = GoodsModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = GoodsModel.SHORT_NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = GoodsModel.SPECIFICATION;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    column = new ColumnConfig();
		name = GoodsModel.TYPE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);

	    column = new ColumnConfig();
		name = GoodsModel.CLASSES;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
	    
	    return configs;
	}

	
	public String getFieldDisplayName(String code) {
		return GoodsFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public GoodsModel createModelInstance() {
		GoodsModel model = new GoodsModel();
		model.setSealFlag(false);
		return model;
	}
}
